
/**
 * 左侧菜单栏
 * 
 * */

function createMenu(menu){
 
 var div=document.getElementById("west");
 var html="<div style='float:left' id='left_menu' class='symenu'>";    
   for(var i=0;i<menu.length;i++){
   	 var mlist=menu[i];
         html+="<div class="+(i?"collapsed":"")+"><span class="+(i?"collapsed":"")+">"+mlist.text+"</span>";
   	  for(var j=0;j<mlist.children.length;j++){
   	      mchild=mlist.children[j];
   	      html+="<a class='current' href='javascript:void(0)' onclick='addTab(\""
   	              +mchild.text+"\",\""+mchild.url+"\",\""+mchild.icon+"\")'>"
   	              +(mchild.icon?"<img src='"+mchild.icon+"'/>":"")+mchild.text+"</a>";
   	   }
   	     html+="</div>";
   }
   div.innerHTML=html+"</div>";
   new SDMenu("left_menu").init();
} 

function addTab(subtitle,url,icon){
	
	if(!$('#tabs').tabs('exists',subtitle)){
		$('#tabs').tabs('add',{
			title:subtitle,
			content:'<iframe  id="reportManageId" height="100%" style="width:100%;height:100%;" src="'
						+ url+ '" frameborder="No" scrolling="No" noresize="noresize"></iframe>',
			 
			closable:true,
			icon:(icon?icon:'')
		});
	}else{
		$('#tabs').tabs('select',subtitle);
		$('#mm-tabupdate').click();
	}
}

function SDMenu(id) {
	
	if (!document.getElementById(id))return false;
	this.menu = document.getElementById(id);
	this.submenus = this.menu.getElementsByTagName("span");
	this.speed = 3;
	this.oneSmOnly = false;
}

SDMenu.prototype.init = function(){
	
	var mainInstance = this;
	for (var i = 0; i < this.submenus.length; i++){
	  var sub=this.submenus[i];	
		  sub.onclick = function(){  
		  if(this.parentNode.className == "collapsed")    
		    mainInstance.expandMenu(this.parentNode); 
		   else mainInstance.collapseMenu(this.parentNode);
		}
	}	
};

SDMenu.prototype.expandMenu = function(submenu){
	 
	var fullHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var links = submenu.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++)
		fullHeight += links[i].offsetHeight;
	var moveBy = Math.round(this.speed * links.length);
	
	var mainInstance = this;
	var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight + moveBy;
		if (newHeight < fullHeight)
			submenu.style.height = newHeight + "px";
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "";
		}
	}, 30);
    this.collapseOthers(submenu);
};

SDMenu.prototype.collapseMenu = function(submenu){
	   
	var minHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var moveBy = Math.round(this.speed * submenu.getElementsByTagName("a").length);
	var mainInstance = this;
	   
	var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight - moveBy;
		if (newHeight > minHeight)
			submenu.style.height = newHeight + "px";
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "collapsed";
		}
	}, 30);
};
 
SDMenu.prototype.collapseOthers = function(submenu){
	if (this.oneSmOnly){
		for (var i = 0; i < this.submenus.length; i++)
			if (this.submenus[i] != submenu && this.submenus[i].className != "collapsed")
				this.collapseMenu(this.submenus[i]);
	}
};
 
